package com.tuanzi.loan.business.service.sms.el;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tuanzi.loan.business.analyzer.BaiduJRAnalyzer;
import com.tuanzi.loan.business.analyzer.DianJinAnalyzer;
import com.tuanzi.loan.business.entity.common.UserInstallDetail;
import com.tuanzi.loan.business.entity.family.UserCreditRecord;
import com.tuanzi.loan.business.service.common.UserInstallDetailService;
import com.tuanzi.loan.business.service.family.UserCreditRecordService;
import com.tuanzi.loan.business.service.sms.vo.SmsSearchResponse;
import com.tuanzi.loan.core.util.SpringContextHolder;
import com.tuanzi.loan.vo.request.family.SmsCondition;
import com.tuanzi.loan.vo.view.family.UserValidateView;

import lombok.Getter;
import lombok.Setter;

public class MatchRuleContext implements Serializable {

	private static final long serialVersionUID = 4181105645197522832L;
	private static final ThreadLocal<MatchRuleContext> MATCH_RULE_CONTEXT = new ThreadLocal<MatchRuleContext>();

	@Getter
	@Setter
	private List<SmsSearchResponse> responses;
	@Getter
	private final SmsCondition request;
	private Map<String /* phone */, List<UserValidateView.BaiduJR>> baiduJRCache;
	private Map<String/* phone */, List<UserValidateView.DianJin>> dianjinCache;
	private Map<String/* phone id */, UserInstallDetail> userInstallDetails;
	private List<String> blacklistPhones;

	public List<UserValidateView.DianJin> getDianjinWithCache(String phone) {
		List<UserValidateView.DianJin> cachedDianjin = dianjinCache.get(phone);

		if (cachedDianjin == null) {
			return Lists.newArrayList();
		}

		return cachedDianjin;
	}

	public List<UserValidateView.BaiduJR> getBaiduJRWithCache(String phone) {
		List<UserValidateView.BaiduJR> cachedBaiduJR = baiduJRCache.get(phone);

		if (cachedBaiduJR == null) {
			return Lists.newArrayList();
		}
		return cachedBaiduJR;
	}

	public UserInstallDetail getUserInstallDetailWithCache(String phoneId) {
		UserInstallDetail cachedUserInstallDetail = userInstallDetails.get(phoneId);

		if (cachedUserInstallDetail == null) {
			return null;
		}
		return cachedUserInstallDetail;
	}

	public void cacheDianjin(List<String> phones) {
		dianjinCache = SpringContextHolder.getBean(UserCreditRecordService.class).findAllDianJinByPhones(phones).stream().collect(Collectors.groupingByConcurrent(
				UserCreditRecord::getPhone, Collectors.mapping(x -> SpringContextHolder.getBean(DianJinAnalyzer.class).analyze(x.getText1()), Collectors.toList())));
	}

	public void cacheBaiduJR(List<String> phones) {
		baiduJRCache = SpringContextHolder.getBean(UserCreditRecordService.class).findAllBaiduJRByPhones(phones).stream().collect(Collectors.groupingByConcurrent(
				UserCreditRecord::getPhone, Collectors.mapping(x -> SpringContextHolder.getBean(BaiduJRAnalyzer.class).analyse(x.getText1()), Collectors.toList())));
	}

	public void cacheUserInstallDetails(List<String> phoneIds) {
		userInstallDetails = SpringContextHolder.getBean(UserInstallDetailService.class).findAllByPhoneIds(phoneIds).stream()
				.collect(Collectors.toMap(UserInstallDetail::getPhoneId, x -> x));
	}

	public void clearCache() {
		this.baiduJRCache.clear();
		this.dianjinCache.clear();
		this.userInstallDetails.clear();
	}

	public MatchRuleContext(SmsCondition request, List<SmsSearchResponse> responses) {
		this.request = request;
		this.responses = responses;
		this.baiduJRCache = Maps.newConcurrentMap();
		this.dianjinCache = Maps.newConcurrentMap();
		this.userInstallDetails = Maps.newConcurrentMap();
	}

	public static final MatchRuleContext getMatchRuleContext() {
		return MATCH_RULE_CONTEXT.get();
	}

	public static final void setMatchRuleContext(MatchRuleContext matchRuleContext) {
		MATCH_RULE_CONTEXT.set(matchRuleContext);
	}

	public static final void removeMatchRuleContext() {
		MATCH_RULE_CONTEXT.remove();
	}

}
